set -e

package_tar_name=$1
package_name=`echo $package_tar_name | sed 's/\(.*\)\.tar\..*/\1/'`
package_name=`echo $package_name | sed 's/-src$//'`
chapter=7
if [ ! -z "$2" ]; then chapter=$2; fi

echo "build start: $package_name $chapter";

pushd /sources
    tar -xvf $package_tar_name
    cd $package_name
    bash /root/projects/lfs/chapter$chapter/$package_name.sh 2>&1 | tee /root/projects/lfs/chapter$chapter/logs/$package_name.log
    cd ..
    rm -rf $package_name
popd

echo "build end: $package_name $chapter";